<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="采购任务Id" prop="purchaseTaskId">
              <el-input v-model="queryParams.purchaseTaskId" placeholder="请输入采购任务Id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="物料Id" prop="materialId">
              <el-input v-model="queryParams.materialId" placeholder="请输入物料Id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="物料编码" prop="materialCode">
              <el-input v-model="queryParams.materialCode" placeholder="请输入物料编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="供应商Id" prop="supplierId">
              <el-input v-model="queryParams.supplierId" placeholder="请输入供应商Id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="提货批次" prop="batchNo">
              <el-input v-model="queryParams.batchNo" placeholder="请输入提货批次" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="提货数量" prop="pickupAmount">
              <el-input v-model="queryParams.pickupAmount" placeholder="请输入提货数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="入库数量" prop="putAmount">
              <el-input v-model="queryParams.putAmount" placeholder="请输入入库数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="合格数量" prop="passAmount">
              <el-input v-model="queryParams.passAmount" placeholder="请输入合格数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="不合格数量" prop="nopassAmount">
              <el-input v-model="queryParams.nopassAmount" placeholder="请输入不合格数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="到货日期" prop="arrivedDate">
              <el-date-picker v-model="queryParams.arrivedDate" clearable type="date" value-format="YYYY-MM-DD" placeholder="请选择到货日期" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['purchase:pickUpPlan:add']" type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['purchase:pickUpPlan:edit']" type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['purchase:pickUpPlan:remove']" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['purchase:pickUpPlan:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="pickupProductList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="true" label="" align="center" prop="pickupProductId" />
        <el-table-column label="采购任务Id" align="center" prop="purchaseTaskId" />
        <el-table-column label="物料Id" align="center" prop="materialId" />
        <el-table-column label="物料编码" align="center" prop="materialCode" />
        <el-table-column label="供应商Id" align="center" prop="supplierId" />
        <el-table-column label="提货批次" align="center" prop="batchNo" />
        <el-table-column label="提货数量" align="center" prop="pickupAmount" />
        <el-table-column label="入库数量" align="center" prop="putAmount" />
        <el-table-column label="合格数量" align="center" prop="passAmount" />
        <el-table-column label="不合格数量" align="center" prop="nopassAmount" />
        <el-table-column label="到货日期" align="center" prop="arrivedDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.arrivedDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['purchase:pickUpPlan:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['purchase:pickUpPlan:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改提货对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="800px" append-to-body>
      <el-form ref="pickupProductFormRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="采购任务Id" prop="purchaseTaskId">
              <el-input v-model="form.purchaseTaskId" placeholder="请输入采购任务Id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料Id" prop="materialId">
              <el-input v-model="form.materialId" placeholder="请输入物料Id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料编码" prop="materialCode">
              <el-input v-model="form.materialCode" placeholder="请输入物料编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商Id" prop="supplierId">
              <el-input v-model="form.supplierId" placeholder="请输入供应商Id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提货批次" prop="batchNo">
              <el-input v-model="form.batchNo" placeholder="请输入提货批次" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提货数量" prop="pickupAmount">
              <el-input v-model="form.pickupAmount" placeholder="请输入提货数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库数量" prop="putAmount">
              <el-input v-model="form.putAmount" placeholder="请输入入库数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合格数量" prop="passAmount">
              <el-input v-model="form.passAmount" placeholder="请输入合格数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="不合格数量" prop="nopassAmount">
              <el-input v-model="form.nopassAmount" placeholder="请输入不合格数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="到货日期" prop="arrivedDate">
              <el-date-picker v-model="form.arrivedDate" clearable type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择到货日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PickupProduct" lang="ts">
import { listPickupProduct, getPickupProduct, delPickupProduct, addPickupProduct, updatePickupProduct } from '@/api/purchase/pickup';
import { PickupProductVO, PickupProductQuery, PickupProductForm } from '@/api/purchase/pickup/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const pickupProductList = ref<PickupProductVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const pickupProductFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: PickupProductForm = {
  pickupProductId: undefined,
  purchaseTaskId: undefined,
  materialId: undefined,
  materialCode: undefined,
  supplierId: undefined,
  batchNo: undefined,
  pickupAmount: undefined,
  putAmount: undefined,
  passAmount: undefined,
  nopassAmount: undefined,
  arrivedDate: undefined,
  remark: undefined
};
const data = reactive<PageData<PickupProductForm, PickupProductQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    purchaseTaskId: undefined,
    materialId: undefined,
    materialCode: undefined,
    supplierId: undefined,
    batchNo: undefined,
    pickupAmount: undefined,
    putAmount: undefined,
    passAmount: undefined,
    nopassAmount: undefined,
    arrivedDate: undefined,
    params: {}
  },
  rules: {
    pickupProductId: [{ required: true, message: '不能为空', trigger: 'blur' }],
    purchaseTaskId: [{ required: true, message: '采购任务Id不能为空', trigger: 'blur' }],
    materialId: [{ required: true, message: '物料Id不能为空', trigger: 'blur' }],
    materialCode: [{ required: true, message: '物料编码不能为空', trigger: 'blur' }],
    supplierId: [{ required: true, message: '供应商Id不能为空', trigger: 'blur' }],
    batchNo: [{ required: true, message: '提货批次不能为空', trigger: 'blur' }],
    pickupAmount: [{ required: true, message: '提货数量不能为空', trigger: 'blur' }],
    putAmount: [{ required: true, message: '入库数量不能为空', trigger: 'blur' }],
    passAmount: [{ required: true, message: '合格数量不能为空', trigger: 'blur' }],
    nopassAmount: [{ required: true, message: '不合格数量不能为空', trigger: 'blur' }],
    arrivedDate: [{ required: true, message: '到货日期不能为空', trigger: 'blur' }],
    remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询提货列表 */
const getList = async () => {
  loading.value = true;
  const res = await listPickupProduct(queryParams.value);
  pickupProductList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  pickupProductFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: PickupProductVO[]) => {
  ids.value = selection.map((item) => item.pickupProductId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加提货';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: PickupProductVO) => {
  reset();
  const _pickupProductId = row?.pickupProductId || ids.value[0];
  const res = await getPickupProduct(_pickupProductId);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改提货';
};

/** 提交按钮 */
const submitForm = () => {
  pickupProductFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.pickupProductId) {
        await updatePickupProduct(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addPickupProduct(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: PickupProductVO) => {
  const _pickupProductIds = row?.pickupProductId || ids.value;
  await proxy?.$modal.confirm('是否确认删除提货编号为"' + _pickupProductIds + '"的数据项？').finally(() => (loading.value = false));
  await delPickupProduct(_pickupProductIds);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'system/pickupProduct/export',
    {
      ...queryParams.value
    },
    `pickupProduct_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
